package com.eduadmin.exception;

import org.apache.poi.EmptyFileException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.thymeleaf.exceptions.TemplateInputException;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class WebExceptionHandler {
    @ExceptionHandler(EmptyFileException.class)
    public String emptyFileException(HttpServletRequest request) {
        //request.setAttribute("javax.servlet.error.status_code", 500);
        request.setAttribute("url", "/adminindex");
        request.setAttribute("message", "文件选择错误，请重新选择！");
        return "errors/error";
    }
    //NumberFormatException
    @ExceptionHandler(NumberFormatException.class)
    public  String numberFormatException(HttpServletRequest request){
        request.setAttribute("url", "/adminindex");
        request.setAttribute("message", "数字格式异常，数据可能不是数字！");
        return "errors/error";
    }
    @ExceptionHandler(CustomException.class)
    public String customException(CustomException exception,HttpServletRequest request){
        request.setAttribute("url", exception.getUrl());
        request.setAttribute("message", exception.getMessage());
        request.setAttribute("errors",exception.getErrors());
        return "errors/error";
    }

}
